package com.sskj.mine.ui.activity;


import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.github.yoojia.inputs.AndroidNextInputs;
import com.github.yoojia.inputs.StaticScheme;
import com.github.yoojia.inputs.WidgetAccess;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.model.HttpHeaders;
import com.sskj.common.util.ClickUtil;
import com.sskj.common.util.SPUtil;
import com.sskj.lib.RConfig;
import com.sskj.lib.SPConfig;
import com.sskj.lib.base.BaseActivity;
import com.sskj.lib.model.room.UserViewModel;
import com.sskj.lib.util.SchemeUtil;
import com.sskj.mine.R;
import com.sskj.mine.R2;
import com.sskj.mine.bean.LoginBean;
import com.sskj.mine.component.DaggerUserDataComponent;
import com.sskj.mine.presenter.MineLoginActivityPresenter;

import javax.inject.Inject;

import butterknife.BindView;

@Route(path = RConfig.MINE_LOGIN)
public class MineLoginActivity extends BaseActivity<MineLoginActivityPresenter> {
    @BindView(R2.id.etAccount)
    EditText etAccount;
    @BindView(R2.id.etPwd)
    EditText etPwd;
    @BindView(R2.id.btSubmit)
    Button btSubmit;
    @BindView(R2.id.tvRegister)
    TextView tvRegister;
    @BindView(R2.id.tvForgetPwd)
    TextView tvForgetPwd;
    @Inject
    UserViewModel userViewModel;


    @Override
    protected int getLayoutId() {
        return R.layout.activity_mine_login;
    }

    @Override
    public MineLoginActivityPresenter getPresenter() {
        return new MineLoginActivityPresenter();
    }

    @Override
    protected void initView() {

        DaggerUserDataComponent.create().inject(this);
        ClickUtil.click(tvRegister, () -> {
            ARouter.getInstance().build(RConfig.MINE_REGISTER).navigation();
        });
        ClickUtil.click(tvForgetPwd, () -> {
            ARouter.getInstance().build(RConfig.MINE_FORWARD_PWD).navigation();
        });
        AndroidNextInputs inputs = new AndroidNextInputs();
        WidgetAccess widgetAccess = new WidgetAccess(this);
        inputs
                .add(widgetAccess.findEditText(R.id.etAccount), StaticScheme.ChineseMobile())
                .add(widgetAccess.findEditText(R.id.etPwd), SchemeUtil.getPwdScheme());
        ClickUtil.click(btSubmit, () -> {
            if (inputs.test()) {
                mPresenter.login(etAccount.getText().toString(), etPwd.getText().toString());
            }
        });
    }


    public void loginSuccess(LoginBean data) {
        SPUtil.put(SPConfig.MOBILE,etAccount.getText().toString());
        SPUtil.put(SPConfig.ACCOUNT, data.getAccount());
        SPUtil.put(SPConfig.TOKEN, data.getToken());
        OkGo.getInstance().addCommonHeaders(new HttpHeaders());
        OkGo.getInstance().getCommonHeaders().clear();
        HttpHeaders httpHeaders = new HttpHeaders();
        httpHeaders.put(SPConfig.ACCOUNT,data.getAccount());
        httpHeaders.put(SPConfig.TOKEN,data.getToken());
        OkGo.getInstance().addCommonHeaders(httpHeaders);
        userViewModel.update();
        finish();
        overridePendingTransition(R.anim.lib_anim_out, R.anim.lib_anim_back);
    }
}
